﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Negocio;
using System.Windows.Controls.Primitives;

namespace Presentacion
{

    public partial class WTest : UserControl
    {
        private static WTest instance;
        private Club club;
        private ObservableCollection<Test> testsList;
        private ObservableCollection<ReferenceMeasure> referencesList;
        private bool openOnce;
        private bool insertEvaluation;
        private bool insertTestInEvaluation;

        private WTest()
        {
            InitializeComponent();
            lvwEvaluations.AddHandler(Thumb.DragDeltaEvent,
new DragDeltaEventHandler(lvwEvaluations_DragDelta),
true);
            lvwTests.AddHandler(Thumb.DragDeltaEvent,
new DragDeltaEventHandler(lvwTests_DragDelta),
true);
            club = Club.getInstance();
            lvwEvaluations.ItemsSource = club.Evaluations;
            tbxSequence.IsEnabled = false;
            referencesList = new ObservableCollection<ReferenceMeasure>();
            testsList = new ObservableCollection<Test>();
            dgReferenceMeasures.ItemsSource = referencesList;
            lvwTests.ItemsSource = testsList;
            openOnce = false;
            insertEvaluation = true;
            insertTestInEvaluation = true;
            cbxAllTests.ItemsSource = club.Tests;
            tbxSequence.Text = "n/a";
        }

        public static WTest getInstance()
        {
            if (instance == null)
            {
                instance = new WTest();
            }
            return instance;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (insertTestInEvaluation)
            {
                Test test = club.newTest(tbxExercise.Text, tbxSequence.Text, tbxMeasureUnit.Text, referencesList);
                IEnumerator<ReferenceMeasure> p = referencesList.GetEnumerator();
                while (p.MoveNext())
                {
                    club.addReferenceMeasureToTest(test, p.Current);
                }
                if (test != null)
                {
                    if (!testsList.Contains(test))
                    {
                        testsList.Add(test);

                        //MessageBox.Show("Test agregado correctamente", "Ingreso", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                referencesList.Clear();
                cleanTestBox();
            }
            else
            {
                if (lvwTests.SelectedIndex != -1)
                {
                    int posLisTest = lvwTests.SelectedIndex;
                    club.updateTest(tbxExercise.Text, tbxSequence.Text, tbxMeasureUnit.Text, referencesList, testsList[posLisTest]);
                    if (testsList[posLisTest] != null)
                    {
                        cbxAllTests.ItemsSource = null;
                        cbxAllTests.ItemsSource = club.Tests;
                        lvwTests.ItemsSource = null;
                        lvwTests.ItemsSource = testsList;
                        MessageBox.Show("Test modificado correctamente", "Modificación", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    referencesList.Clear();
                    cleanTestBox();
                }
            }
        }

        private void ckbSequence_Checked(object sender, RoutedEventArgs e)
        {
            tbxSequence.IsEnabled = false;
            btnSequence.IsEnabled = true;
        }

        private void ckbSequence_Unchecked(object sender, RoutedEventArgs e)
        {
            tbxSequence.IsEnabled = false;
            btnSequence.IsEnabled = false;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (insertEvaluation)
            {
                Evaluation evaluation = club.newEvaluation(tbxEvaluationName.Text, tbxDescription.Text, testsList);

                if (evaluation != null)
                {
                    IEnumerator<Test> p = testsList.GetEnumerator();
                    while (p.MoveNext())
                    {
                        club.addTestToEvaluation(evaluation, p.Current);
                        club.addTest(p.Current);
                    }

                    if (club.addEvaluation(evaluation) == true)
                    {
                        MessageBox.Show("Evaluación creada correctamente", "Ingreso", MessageBoxButton.OK, MessageBoxImage.Information);
                        testsList.Clear();
                    }
                    else
                    {
                        MessageBox.Show("La evaluación ya fue ingresada en el sistema", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }                
            }
            else
            {
                int pos = lvwEvaluations.SelectedIndex;
                IEnumerator<Test> p = testsList.GetEnumerator();
                if (pos != -1)
                {
                    club.updateEvaluation(tbxEvaluationName.Text, tbxDescription.Text, testsList, club.Evaluations[pos]);
                    if (club.Evaluations[pos] == null)
                    {
                        MessageBox.Show("Datos incorrectos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        if (testsList.Count != 0)
                        {
                            Test test = testsList[testsList.Count - 1];
                            club.addTest(test);
                        }
                        MessageBox.Show("Evaluación modificada correctamente", "Modificación", MessageBoxButton.OK, MessageBoxImage.Information);
                        lvwEvaluations.ItemsSource = null;
                        testsList.Clear();
                        tbxEvaluationName.Text = "";
                        lvwEvaluations.ItemsSource = club.Evaluations;
                    }                    
                }                
            }
            cleanEvaluationBox();
            insertEvaluation = true;
        }

        private void lvwEvaluations_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                int pos = lvwEvaluations.SelectedIndex;
                tbxEvaluationName.Text = club.Evaluations[pos].Name;
                tbxDescription.Text = club.Evaluations[pos].Description;
                IEnumerator<Test> p = club.Evaluations[pos].Tests.GetEnumerator();
                testsList.Clear();
                cleanTestBox();
                while (p.MoveNext())
                {
                    testsList.Add(p.Current);
                }
                insertEvaluation = false;
            }
            catch (Exception) { }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lvwEvaluations.SelectedIndex != -1)
            {
                int posList = lvwEvaluations.SelectedIndex;
                club.Evaluations[posList].removed();
                club.Evaluations.RemoveAt(posList);
                testsList.Clear();
                club.Tests.Clear();
                club.loadTests();
                dgReferenceMeasures.ItemsSource = null;
                dgReferenceMeasures.ItemsSource = referencesList;
                cleanEvaluationBox();
                cleanTestBox();
            }

        }

        private void btnDeleteTest_Click(object sender, RoutedEventArgs e)
        {
            if (lvwTests.SelectedIndex != -1)
            {
                int posLisTest = lvwTests.SelectedIndex;
                int posListEvaluation = lvwEvaluations.SelectedIndex;
                testsList.RemoveAt(posLisTest);                
                club.Evaluations[posListEvaluation].updated();
            }
        }

        private void lvwTests_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvwTests.SelectedIndex != -1)
            {
                dgReferenceMeasures.ItemsSource = null;
                int posListTest = lvwTests.SelectedIndex;
                tbxExercise.Text = testsList[posListTest].Name;
                tbxSequence.Text = testsList[posListTest].Sequence;
                tbxMeasureUnit.Text = testsList[posListTest].MeasureUnit;                
                this.referencesList.Clear();
                for (int i = 0; i < testsList[posListTest].ReferenceMeasures.Count; i++)
                {
                    this.referencesList.Add(testsList[posListTest].ReferenceMeasures[i]);
                }
                dgReferenceMeasures.ItemsSource = this.referencesList;
                insertTestInEvaluation = false;
            }
        }

        private void cleanEvaluationBox()
        {
            this.tbxEvaluationName.Clear();
            this.tbxDescription.Clear();
            insertEvaluation = true;
        }

        private void cleanTestBox()
        {
            this.tbxExercise.Clear();
            this.tbxSequence.Clear();
            this.tbxMeasureUnit.Clear();
            tbxSequence.Text = "n/a";
            insertTestInEvaluation = true;
            referencesList.Clear();            
        }

        private void btnSequence_Click(object sender, RoutedEventArgs e)
        {
            string sequenceStr = "";
            ObservableCollection<Test> testListSec = new ObservableCollection<Test>();
            for (int i = 0; i < testsList.Count; i++)
            {
                testListSec.Add(testsList[i]);
            }
            WSequence sequence = new WSequence(sequenceStr, testListSec, tbxSequence);
            sequence.Show();
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            if (lvwTests.Items.Count != 0)
            {
                lvwTests.SelectedIndex = club.changeTestPosition(testsList, lvwTests.SelectedIndex, true);
                lvwTests.ItemsSource = null;
                lvwTests.ItemsSource = testsList;           
            }
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            if (lvwTests.Items.Count != 0)
            {
                lvwTests.SelectedIndex = club.changeTestPosition(testsList, lvwTests.SelectedIndex, false);
                lvwTests.ItemsSource = null;
                lvwTests.ItemsSource = testsList;
            }
        }

        private void lvwEvaluations_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb senderAsThumb = e.OriginalSource as Thumb;
            GridViewColumnHeader header
              = senderAsThumb.TemplatedParent as GridViewColumnHeader;
            if (header != null)
            {
                if (header.Column.ActualWidth < 339.224)
                {
                    header.Column.Width = 339.224;
                }
                if (header.Column.ActualWidth > 339.224)
                {
                    header.Column.Width = 339.224;
                }
            }
        }

        private void lvwTests_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb senderAsThumb = e.OriginalSource as Thumb;
            GridViewColumnHeader header
              = senderAsThumb.TemplatedParent as GridViewColumnHeader;
            if (header != null)
            {
                if (header.Column.ActualWidth < 339.224)
                {
                    header.Column.Width = 339.224;
                }
                if (header.Column.ActualWidth > 339.224)
                {
                    header.Column.Width = 339.224;
                }
            }
        }

        private void btnCleanTest_Click(object sender, RoutedEventArgs e)
        {
            cleanTestBox();
        }

        private void btnCleanEvaluation_Click(object sender, RoutedEventArgs e)
        {
            cleanTestBox();
            cleanEvaluationBox();
            testsList.Clear();            
        }

        private void btnAddTest_Click(object sender, RoutedEventArgs e)
        {
            openOnce = true;
            Test test = (Test)cbxAllTests.SelectedItem;
            if (!testsList.Contains(test) && openOnce == true)
            {
                testsList.Add(test);
            }            
        }




    }
}
